package com.situ.library.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.situ.library.pojo.entity.Book;
import com.situ.library.pojo.query.BookQuery;
import com.situ.library.service.IBookService;
import com.situ.library.service.impl.BookServiceImpl;
import com.situ.library.util.JSONUtils;
import com.situ.library.util.MyBeanUtils;
import com.situ.library.util.PageInfo;
import com.situ.library.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

import static com.situ.library.util.JSONUtils.toJSON;

@WebServlet("/book")
public class BookController extends HttpServlet {
    private IBookService bookService = new BookServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case "selectAll":
                selectAll(req, resp);
                break;
            case "exportExcel":
                exportExcel(req, resp);
                break;
            case "importExcel":
                importExcel(req, resp);
                break;
        }
    }

    private void importExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BookController.importExcel");
        bookService.importExcel(req);
        toJSON(resp, Result.ok("上传成功"));
    }

    private void exportExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogController.exportExcel");
        bookService.exportExcel(resp);
    }


    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BookController.selectAll");
        List<Book> list = bookService.selectAll();
        toJSON(resp, Result.ok(list));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BookServlet.update");
        Book book = MyBeanUtils.copyToBean(req, Book.class);
        bookService.update(book);
        toJSON(resp, Result.ok("更新成功"));
    }



    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BookServlet.selectById");
        String id = req.getParameter("id");
        Book book = bookService.selectById(Integer.parseInt(id));
        toJSON(resp, Result.ok(book));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BookServlet.add");
        Book book = MyBeanUtils.copyToBean(req, Book.class);
        bookService.add(book);
        toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BookServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        bookService.deleteAll(ids);
        toJSON(resp, Result.ok("删除成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BookServlet.deleteById");
        String id = req.getParameter("id");
        bookService.deleteById(Integer.parseInt(id));
        toJSON(resp, Result.ok("删除成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BookServlet.selectByPage");
        BookQuery bookQuery = MyBeanUtils.copyToBean(req, BookQuery.class);
        PageInfo pageInfo = bookService.selectByPage(bookQuery);

        toJSON(resp, pageInfo);
    }

}